<?php
function postBetToXBet ($out = 0)
{
    global $g_gametype_3d;
    getdb("update k_config set value = 1 Where id = 'systemcheck' LIMIT 1");
    getdb("update k_config set value = 1 Where id = 'paper_post_flag' LIMIT 1");
    $game_Arr = $g_gametype_3d;
    unset($game_Arr[0]);
    $bet_Arr = array("bet", "bet2", "bet8");
    $status_Arr = array();
    foreach ($game_Arr as $gametype => $gameArr)
    {
        $tbl = "_" . $gameArr['en'];
        foreach ($bet_Arr as $key => $bet)
        {
            $SrcBet = $bet . $tbl;
            $TagBet = "x" . $SrcBet;
            if (1)
            {
                $theDay = NULL;
                $rs = getdb("!!select open from lotto{$tbl} where id = (SELECT min(lotto_id) FROM {$SrcBet} A LEFT JOIN lotto{$tbl} B ON B.id=A.lotto_id  WHERE date(B.open)<curdate())");
                if ($rs->eof)
                {
                    $status_Arr[$gametype] = 1;
                    break;
                }
                $theDay = $rs->f[0];
                $rs->close();
                $rs = getdb("!!select count(*) from {$SrcBet} where lotto_id in (SELECT id FROM lotto{$tbl} WHERE open='{$theDay}')");
                $SrcBetRecords = $rs->f[0];
                $rs = getdb("!!select count(*) from {$TagBet} where lotto_id in (SELECT id FROM lotto{$tbl} WHERE open='{$theDay}')");
                $TagBetRecords = $rs->f[0];
                if ($SrcBetRecords == 0 && $TagBetRecords == 0)
                {
                    $status_Arr[$gametype] = 1;
                    break;
                }
                if ($SrcBetRecords == 0 && 0 < $TagBetRecords)
                {
                    $status_Arr[$gametype] = 1;
                    break;
                }
                if (0 < $SrcBetRecords && $TagBetRecords == 0)
                {
                    switch ($key)
                    {
                        case 0 :
                            if ($gameArr['type'] == 3)
                            {
                                $fieldStr = "`id`, `user_id`, `lotto_id`, `createtime`, `bettype` ,`xyid`, `subid`, `disk`,`numbers`, `line_nums`, `amount`, `unitmoney`, `payoff`, `rate`, `rate1`, `rate2`, `rate3`, `rate4`, `rate5`, `a1`, `b1`, `a2`, `b2`, `c2`, `d2`, `e2`, `r2`, `a3`, `b3`, `c3`, `d3`, `e3`, `r3`, `a4`, `b4`, `c4`, `d4`, `e4`, `r4`, `a5`, `b5`, `c5`, `d5`, `e5`, `g5`, `r5`, `r6`, `percent2`, `percent3`, `percent4`, `percent5`, `percent255`, `back1`, `back2`, `back3`, `back4`, `back5`, `back6`, `rame`, `state`, `ip`, `sessid`,`acl2`,`acl3`,`acl4`,`acl5` ";
                                $sqlCmd = "INSERT INTO {$TagBet} ({$fieldStr})" . " SELECT {$fieldStr} FROM {$SrcBet} WHERE lotto_id IN (SELECT id FROM lotto{$tbl} WHERE open='{$theDay}')";
                            }
                            else
                            {
                                $fieldStr = "`id`, `user_id`, `lotto_id`, `createtime`, `bettype` , `subid`, `numbers`, `line_nums`, `amount`, `unitmoney`, `payoff`, `rate`, `rate1`, `rate2`, `rate3`, `rate4`, `rate5`, `a1`, `b1`, `a2`, `b2`, `c2`, `d2`, `e2`, `r2`, `a3`, `b3`, `c3`, `d3`, `e3`, `r3`, `a4`, `b4`, `c4`, `d4`, `e4`, `r4`, `a5`, `b5`, `c5`, `d5`, `e5`, `g5`, `r5`, `r6`, `disk`, `percent2`, `percent3`, `percent4`, `percent5`, `percent255`, `back1`, `back2`, `back3`, `back4`, `back5`, `back6`, `rame`, `state`, `ip`, `sessid`,`acl2`,`acl3`,`acl4`,`acl5` ";
                                $sqlCmd = "INSERT INTO {$TagBet} ({$fieldStr})" . " SELECT {$fieldStr} FROM {$SrcBet} WHERE lotto_id IN (SELECT id FROM lotto{$tbl} WHERE open='{$theDay}')";
                            }
                            break;
                        case 1 :
                            if ($gameArr['type'] == 3)
                            {
                                $fieldStr = "`id`, `user_id`, `prev_id`, `uperid3`, `uperid4`, `uperid5`, `lotto_id`, `createtime`, `acl`, `bettype`, `xyid`, `subid`,`numbers`, `line_nums`, `amount`, `unitmoney`, `rate`, `rate2`, `rate3`, `rate4`, `rate5`, `a2`, `b2`, `r2`, `a3`, `b3`, `c3`, `d3`, `e3`, `r3`, `a4`, `b4`, `c4`, `d4`, `e4`, `r4`, `a5`, `b5`, `c5`, `d5`, `e5`, `g5`, `r5`, `r6`, `percent3`, `percent4`, `percent5`, `percent255`, `back2`, `back3`, `back4`, `back5`, `back6`, `betmode`, `rame`, `state`, `ip`";
                                $sqlCmd = "INSERT INTO {$TagBet} ({$fieldStr})" . " SELECT {$fieldStr} FROM {$SrcBet} WHERE lotto_id IN (SELECT id FROM lotto{$tbl} WHERE open='{$theDay}')";
                            }
                            else
                            {
                                $fieldStr = "`id`, `user_id`, `prev_id`, `uperid3`, `uperid4`, `uperid5`, `lotto_id`, `createtime`, `acl`, `bettype`, `subid`, `numbers`, `line_nums`, `amount`, `unitmoney`, `rate`, `rate2`, `rate3`, `rate4`, `rate5`, `a2`, `b2`, `r2`, `a3`, `b3`, `c3`, `d3`, `e3`, `r3`, `a4`, `b4`, `c4`, `d4`, `e4`, `r4`, `a5`, `b5`, `c5`, `d5`, `e5`, `g5`, `r5`, `r6`, `percent3`, `percent4`, `percent5`, `percent255`, `back2`, `back3`, `back4`, `back5`, `back6`, `betmode`, `rame`, `state`, `ip`";
                                $sqlCmd = "INSERT INTO {$TagBet} ({$fieldStr})" . " SELECT {$fieldStr} FROM {$SrcBet} WHERE lotto_id IN (SELECT id FROM lotto{$tbl} WHERE open='{$theDay}')";
                            }
                            break;
                        case 2 :
                            if ($gameArr['type'] == 3)
                            {
                                $fieldStr = "`id`, `lotto_id`, `createtime`, `bettype`, `xyid`, `subid`, `numbers`, `line_nums`, `amount`, `rate`, `r5`, `back`, `rame`, `account`, `state`, `ip` ";
                                $sqlCmd = "INSERT INTO {$TagBet} ({$fieldStr})" . " SELECT {$fieldStr} FROM {$SrcBet} WHERE lotto_id IN (SELECT id FROM lotto{$tbl} WHERE open='{$theDay}')";
                            }
                            else
                            {
                                $fieldStr = "`id`, `lotto_id`, `createtime`, `bettype` , `subid`, `numbers`, `line_nums`, `amount`, `rate`, `r5`, `back`, `rame`, `account`, `state`, `ip` ";
                                $sqlCmd = "INSERT INTO {$TagBet} ({$fieldStr})" . " SELECT {$fieldStr} FROM {$SrcBet} WHERE lotto_id IN (SELECT id FROM lotto{$tbl} WHERE open='{$theDay}')";
                            }
                            break;
                    }
                    getdb($sqlCmd);
                }
                else if (0 < $SrcBetRecords && $SrcBetRecords != $TagBetRecords && 0 < $TagBetRecords)
                {
                    $status_Arr[$gametype] = 0;
                    $sqlCmd = "DELETE FROM {$TagBet} WHERE lotto_id in (SELECT id FROM lotto{$tbl} WHERE open='{$theDay}')";
                    getdb($sqlCmd);
                }
                else if (0 < $SrcBetRecords && $SrcBetRecords == $TagBetRecords)
                {
                    $sqlCmd = "DELETE FROM {$SrcBet} WHERE lotto_id IN (SELECT id FROM lotto{$tbl} WHERE open='{$theDay}')";
                    getdb($sqlCmd);
                }
                else
                {
                    break;
                }
            }
        }
    }
    if (!in_array(0, $status_Arr))
    {
        getdb("update k_config set value = 0 Where id = 'systemcheck' LIMIT 1");
        getdb("update k_config set value = 0 Where id = 'paper_post_flag' LIMIT 1");
        foreach ($game_Arr as $gametype => $gameArr)
        {
            $tbl = "_" . $gameArr['en'];
            foreach ($bet_Arr as $key => $bet)
            {
                $SrcBet = $bet . $tbl;
                $rs = getdb("!!Select count(*) from {$SrcBet}");
                if ($rs->f[0] == 0)
                {
                    getdb("TRUNCATE TABLE {$SrcBet}");
                }
                else
                {
                    getdb("OPTIMIZE TABLE {$SrcBet}");
                }
            }
        }
        return "";
    }
    return "过帐未完成";
}

function postBetToSummary ($open = "0000-00-00", $gameid = -1)
{
    global $g_gametype_3d;
    getdb("update k_config set value = 1 Where id = 'paper_post_flag' LIMIT 1");
    $bet_Arr = array("bet");
    $result = "";
    foreach ($g_gametype_3d as $gametype => $gameArr)
    {
        if (0 - 1 < $gameid && $gametype != $gameid)
        {
            continue;
        }
        $tbl = $gametype ? "_" . $gameArr['en'] : "";
        foreach ($bet_Arr as $key => $bet)
        {
            $SrcBet = ( $gametype ? "x" : "" ) . $bet . $tbl;
            $TagBet = "s" . $bet . $tbl;
            if (!chkSummaryTable($TagBet))
            {
                return "!错误, 产生汇总清单资料表({$TagBet})异常!";
            }
            $sql = "SELECT count(A.id) FROM {$SrcBet} A LEFT JOIN lotto{$tbl} B ON B.id=A.lotto_id WHERE B.open='{$open}'";
            $rs = getdb($sql);
            $result .= "<li>{$SrcBet} ({$rs->f[0]}) => {$TagBet} ";
            if (0 < $rs->f[0])
            {
                $sql = "DELETE FROM {$TagBet} WHERE open = '{$open}'";
                getdb($sql);
                if ($TagBet == "sbet")
                {
                    $percentArr = array("mpercent", "percent2", "percent3", "percent4", "upercent");
                }
                else
                {
                    $percentArr = array("percent2", "percent3", "percent4", "percent5", "percent255");
                }
                $sql = "INSERT INTO {$TagBet} " . "SELECT `user_id`,'{$open}' as open" . ",COUNT(*) as records,SUM(A.`amount`) AS AMOUNT1,SUM(case when (A.a1=0 and A.b1=0) then 0 else A.`amount` end) AS AMOUNT2" . ",SUM(A.a1) as a1,SUM(A.b1) as b1,SUM(A.a2) as a2,SUM(A.b2) as b2,SUM(A.c2) as c2,SUM(A.d2) as d2,SUM(A.e2) as e2,SUM(A.r2) as r2" . ",SUM(A.a3) as a3,SUM(A.b3) as b3,SUM(A.c3) as c3,SUM(A.d3) as d3,SUM(A.e3) as e3,SUM(A.r3) as r3" . ",SUM(A.a4) as a4,SUM(A.b4) as b4,SUM(A.c4) as c4,SUM(A.d4) as d4,SUM(A.e4) as e4,SUM(A.r4) as r4" . ",SUM(A.a5) as a5,SUM(A.b5) as b5,SUM(A.c5) as c5,SUM(A.d5) as d5,SUM(A.e5) as e5,SUM(A.r5) as r5" . ",SUM(A.g5) as g5,SUM(A.r6) as r6" . ",AVG({$percentArr[0]}) as {$percentArr[0]},AVG({$percentArr[1]}) as {$percentArr[1]},AVG({$percentArr[2]}) as {$percentArr[2]},AVG({$percentArr[3]}) as {$percentArr[3]},AVG({$percentArr[4]}) as {$percentArr[4]}" . ",acl2,acl3,acl4,acl5 " . "FROM {$SrcBet} A LEFT JOIN lotto{$tbl} B ON B.id=A.lotto_id WHERE B.open='{$open}' " . "group BY A.`user_id`,B.open,acl2,acl3,acl4,acl5 ORDER BY A.`user_id`,B.open";
                if (getdb($sql))
                {
                    $sql = "SELECT COUNT(*) as cc, SUM(records) as records FROM {$TagBet} WHERE open = '{$open}'";
                    $rs1 = getdb($sql);
                    $result .= "({$rs1->f[0]} / {$rs1->f[1]})</li>";
                    $rs1->close();
                }
                else
                {
                    $result .= " 失败.</li>";
                }
            }
            $rs->close();
        }
    }
    getdb("update k_config set value = 0 Where id = 'paper_post_flag' LIMIT 1");
    return $result . "<br>";
}

function chkSummaryTable ($table = "")
{
    if ($table == "" || strlen($table) < 4)
    {
        return false;
    }
    if ($table == "sbet")
    {
        $percentArr = array("mpercent", "percent2", "percent3", "percent4", "upercent");
    }
    else
    {
        $percentArr = array("percent2", "percent3", "percent4", "percent5", "percent255");
    }
    $sql = "CREATE TABLE IF NOT EXISTS {$table} (" . "user_id int(6)," . "open date DEFAULT '0000-00-00' NOT NULL," . "records int(9) DEFAULT '0' NOT NULL," . "amount1 int(9) DEFAULT '0' NOT NULL," . "amount2 int(9) DEFAULT '0' NOT NULL," . "a1 float(12,2) DEFAULT '0.00' NOT NULL," . "b1 float(12,2) DEFAULT '0.00' NOT NULL," . "a2 float(12,3) DEFAULT '0.000' NOT NULL," . "b2 float(12,2) DEFAULT '0.00' NOT NULL," . "c2 float(12,2) DEFAULT '0.00' NOT NULL," . "d2 float(12,2) DEFAULT '0.00' NOT NULL," . "e2 float(12,3) DEFAULT '0.000' NOT NULL," . "r2 float(11,3) DEFAULT '0.000' NOT NULL," . "a3 float(12,3) DEFAULT '0.000' NOT NULL," . "b3 float(12,2) DEFAULT '0.00' NOT NULL," . "c3 float(12,2) DEFAULT '0.00' NOT NULL," . "d3 float(12,2) DEFAULT '0.00' NOT NULL," . "e3 float(12,3) DEFAULT '0.000' NOT NULL," . "r3 float(11,3) DEFAULT '0.000' NOT NULL," . "a4 float(12,3) DEFAULT '0.000' NOT NULL," . "b4 float(12,2) DEFAULT '0.00' NOT NULL," . "c4 float(12,2) DEFAULT '0.00' NOT NULL," . "d4 float(12,2) DEFAULT '0.00' NOT NULL," . "e4 float(12,3) DEFAULT '0.000' NOT NULL," . "r4 float(11,3) DEFAULT '0.000' NOT NULL," . "a5 float(12,3) DEFAULT '0.000' NOT NULL," . "b5 float(12,2) DEFAULT '0.00' NOT NULL," . "c5 float(12,2) DEFAULT '0.00' NOT NULL," . "d5 float(12,2) DEFAULT '0.00' NOT NULL," . "e5 float(12,3) DEFAULT '0.000' NOT NULL," . "g5 float(12,3) DEFAULT '0.000' NOT NULL," . "r5 float(12,2) DEFAULT '0.00' NOT NULL," . "r6 float(12,3) DEFAULT '0.000' NOT NULL," . "{$percentArr[0]} int(3)," . "{$percentArr[1]} int(3)," . "{$percentArr[2]} int(3)," . "{$percentArr[3]} int(3)," . "{$percentArr[4]} int(3)," . "acl2 int(6)," . "acl3 int(6)," . "acl4 int(6)," . "acl5 int(6)," . "PRIMARY KEY (user_id,open)," . "KEY user_id (user_id)," . "KEY open (open)" . ") ENGINE=MyISAM;";
    return getdb($sql);
}

?>